package com.imessage.action.base.config;

import com.bean.BaseConfig;
import com.imessage.comm.Constants;
import com.imessage.type.BaseConfigCode;
import com.imessage.vo.ConfigVo;
import com.imessage.web.HttpJson;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/config")
public class ConfigAction extends ConfigBaseAction{

	@RequestMapping(value = "")
	public Object run(ConfigVo configVo){
		List<Expression> expressionList=new ArrayList<Expression>();
		setExpressionList(expressionList, configVo);
		Page<BaseConfig> page=baseService.findPage(BaseConfig.class, expressionList,
				null, getOrder(configVo.getOrders()),
				configVo.getPageCurrent(), configVo.getPageSize());
		return page;
	}

	@RequestMapping(value = "initpage")
	public ModelAndView initpage(
			@RequestParam(name="req_id",required = true)String id){
		ModelAndView modelAndView = new ModelAndView();
		BaseConfig baseConfig= new BaseConfig();
		if(StringUtils.isNotBlank(id)){
			baseConfig=baseService.findById(BaseConfig.class,id);
			modelAndView.setViewName("admin/base/config/modify");
		}else{
			modelAndView.setViewName("admin/base/config/add");
		}
		setModelAndView(modelAndView,baseConfig);
		return modelAndView;
	}

	@RequestMapping(value = "add")
	public Object add(ConfigVo configVo) {
		if(existConfig(configVo.getCode(),null)){
			return getJsonForErr("编码重复", datagrid);
		}
		BaseConfig baseConfig=new BaseConfig();
		setBaseConfig(configVo, baseConfig);
        baseConfig.setCode(BaseConfigCode.fromValue(configVo.getCode()));
		int state= baseService.saveObject(baseConfig);
		if(state==Constants.STATE_OPERATOR_SUCC){
			return baseConfig;
		}
		return getJsonForErr("", datagrid);
	}

	@RequestMapping(value = "modify")
	public Object modify(ConfigVo configVo) {
		if(existConfig(configVo.getCode(),configVo.getId())){
			return getJsonForErr("编码重复", datagrid);
		}
		BaseConfig baseConfig=baseService.findById(BaseConfig.class,configVo.getId());
		setBaseConfig(configVo, baseConfig);
        baseConfig.setCode(BaseConfigCode.fromValue(configVo.getCode()));
		int state= baseService.updateObject(baseConfig);
		if(state==Constants.STATE_OPERATOR_SUCC){
			return baseConfig;
		}
		return getJsonForErr("", datagrid);
	}
	@RequestMapping(value = "del")
	public Object del(@RequestParam(name="id",required = true)String ids) {
		int state=Constants.STATE_OPERATOR_LOST;
		for(String id:ids.split(",")){
			BaseConfig baseConfig=baseService.findById(BaseConfig.class,id);
			state=baseService.delObject(baseConfig);
		}
		if(state==Constants.STATE_OPERATOR_SUCC){
			return getJsonForSucc("", datagrid);
		}
		return getJsonForErr("", datagrid);
	}


}
